/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.commonlibs.http.interceptor;

import androidx.annotation.NonNull;

import com.ldc.commonlibs.utils.DataSourceUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author liudc
 * @since 2022/7/10 22:04
 */
public class CookieInterceptor implements Interceptor {
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();
        return chain.proceed(buildRequest(request));
    }

    private Request buildRequest(Request request) {
        if (!request.url().encodedPath().contentEquals("/login/cellphone")) {
            String[] cookieList = DataSourceUtil.getCookie().split(";");
            Request.Builder builder = request.newBuilder();
            for (String cookie : cookieList) {
                builder.addHeader("Cookie", cookie);
            }
            return builder.build();
        }
        return request;
    }
}
